package com.xja.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xja.model.entity.Collection;
import com.xja.model.vo.req.CollectionReqVO;
import com.xja.model.vo.req.CollectionTestReqVO;
import com.xja.model.vo.resp.CollectionTestRespVO;
import com.xja.model.vo.resp.Result;
import com.xja.service.ICollectionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author caijg
 * @since 2025-06-10
 */
@RestController
@RequestMapping("/admin/collection")
@Slf4j
@Tag(name = "知识库管理")
@RequiredArgsConstructor
public class AdminCollectionController {

    private final ICollectionService collectionService;

    @GetMapping("/search")
    @Operation(summary = "分页查询")
    public Result<Page<Collection>> search(CollectionReqVO collectionReqVO) {
        log.info("分页查询：{}", collectionReqVO);
        Page<Collection> pageResult = collectionService.search(collectionReqVO);
        return Result.success(pageResult);
    }

    @PostMapping
    @Operation(summary = "新增文档")
    public Result<Void> add(@RequestBody Collection collection) {
        log.info("新增文档：{}", collection);
        collectionService.add(collection.getContent());
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除文档")
    public Result<Void> delete(@PathVariable String id) {
        log.info("删除文档：{}", id);
        collectionService.deleteById(id);
        return Result.success();
    }

    @PutMapping
    @Operation(summary = "更新文档")
    public Result<Void> update(@RequestBody Collection collection) {
        log.info("更新文档：{}", collection);
        collectionService.updateCollection(collection);
        return Result.success();
    }

    @GetMapping("/test")
    @Operation(summary = "测试")
    public Result<List<CollectionTestRespVO>> test(CollectionTestReqVO collectionTestReqVO) {
        log.info("测试：{}", collectionTestReqVO);
        List<CollectionTestRespVO> collectionTestRespVOList = collectionService.test(collectionTestReqVO);
        return Result.success(collectionTestRespVOList);
    }
}